package com.yxq.mdt.app.modules.socialcontact.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.ui.activity.MainPersonalEditData;
import com.yxq.mdt.app.modules.socialcontact.adapter.ExamplePagerAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.NearManAndDynamicBean;
import com.yxq.mdt.app.modules.socialcontact.bean.NearManAndDynamicTopicBean;
import com.yxq.mdt.app.modules.socialcontact.bean.PerfectBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SelectNearbyListBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialNearContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialNearPresenter;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.main.BeInterestedListFragment;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.main.NearbyListFragment;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.main.NearbyManListFragment;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.main.NearbyWoListFragment;
import com.yxq.mdt.app.widgets.NonSwipeableViewPager;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 附近的人
 */
public class NearbyListActivity extends BaseActivity<MainSocialNearPresenter> implements MainSocialNearContract.View {
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    private static final String[] CHANNELS = new String[]{"不限", "男生", "女生"};
    @BindView(R.id.nearby_view_pager)
    NonSwipeableViewPager nearbyViewPager;
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.nearby_view_tv)
    TextView nearbyViewTv;
    @BindView(R.id.nearby_view_lint)
    RelativeLayout nearbyViewLint;
    private List<String> strings = Arrays.asList(CHANNELS);
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private ExamplePagerAdapter mExamplePagerAdapter;
    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
    private List<NearManAndDynamicBean> nearManAndDynamicBeans;
    PerfectBean perfectBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_nearby_details;
    }

    @Override
    public void initView() {
        initFragments();
        //初始化场次列表菜单
        nearManAndDynamicBeans = new ArrayList<>();
        mExamplePagerAdapter = new ExamplePagerAdapter(getSupportFragmentManager(), mFragments, strings);
        mFragmentContainerHelper.handlePageSelected(0, false);
        nearbyViewPager.setAdapter(mExamplePagerAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return strings.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(mActivity);
                commonPagerTitleView.setContentView(R.layout.layout_nearby_item_layout);
                final TextView titleTitle = commonPagerTitleView.findViewById(R.id.indicator_tv_title);
                final ImageView sub = commonPagerTitleView.findViewById(R.id.indicator_tv_sub);
                titleTitle.setText(strings.get(index));
                switch (index) {
                    case 0:
                        sub.setVisibility(View.GONE);
                        break;
                    case 1:
                        sub.setImageResource(R.mipmap.xb_nan);
                        break;
                    case 2:
                        sub.setImageResource(R.mipmap.xb_nv);
                        break;
                }
                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {

                    @Override
                    public void onSelected(int index, int totalCount) {
                        titleTitle.setTextSize(20);
                        titleTitle.setTextColor(getResources().getColor(R.color.color_font_black));
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        titleTitle.setTextSize(15);
                        titleTitle.setTextColor(getResources().getColor(R.color.color_font_black));
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {

                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {

                    }
                });
                commonPagerTitleView.setOnClickListener(v -> {
                    nearbyViewPager.setCurrentItem(index);
                });


                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(mActivity, 0);
            }
        });
        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(0.1f));
        fragmentContainerHelper.setDuration(0);
        nearbyViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
        nearbyViewTv.setOnClickListener(view -> {
            MyPersonalBean personalBean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
            Intent it = new Intent(mActivity, MainPersonalEditData.class);
            it.putExtra("userId", personalBean.getUserId() + "");
            mActivity.startActivity(it);
        });
        nearbyViewPager.setOffscreenPageLimit(3);
        if (Hawk.get(SPKey.SP_KEY_USER_PERFECTTATIO_NUMBER) != null) {
            if (Hawk.get(SPKey.SP_KEY_USER_PERFECTTATIO_NUMBER).equals("100")) {
                nearbyViewLint.setVisibility(View.GONE);
            } else {
                nearbyViewLint.setVisibility(View.VISIBLE);
            }
        }


    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new MainSocialNearPresenter(this, new MainSocialContactModel());
        mPresenter.getQueryIsPerfect();
    }

    //初始化fragment addFragment
    private void initFragments() {
        //不限
        NearbyListFragment beInterestedListFragment = new NearbyListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("isMenAndWomen", "0");
        beInterestedListFragment.setArguments(bundle);
        mFragments.add(beInterestedListFragment);
        //男生
        NearbyManListFragment nearbyManListFragment = new NearbyManListFragment();
        Bundle bundleMan = new Bundle();
        bundleMan.putString("isMenAndWomen", "1");
        nearbyManListFragment.setArguments(bundleMan);
        mFragments.add(nearbyManListFragment);
        //女生
        NearbyWoListFragment nearbyWoListFragment = new NearbyWoListFragment();
        Bundle bundleWomen = new Bundle();
        bundleWomen.putString("isMenAndWomen", "2");
        nearbyWoListFragment.setArguments(bundleWomen);
        mFragments.add(nearbyWoListFragment);
    }

    @Override
    public void onLoadNearManAndDynamicSuccess(String s) {
        if (TextUtils.isEmpty(s)) {
            return;
        } else {
            List<NearManAndDynamicBean> list = JSON.parseArray(s, NearManAndDynamicBean.class);
            nearManAndDynamicBeans.addAll(list);
        }
    }

    @Override
    public void onLoadNearManAndDynamicFailed() {

    }

    @Override
    public void onLoadSelectMyNearbySuccess(SelectNearbyListBean selectNearbyListBean) {

    }

    @Override
    public void onLoadSelectMyNearbyFailed() {

    }

    @Override
    public void onLoadNearManAndDynamicTopicSuccess(NearManAndDynamicTopicBean nearManAndDynamicTopicBean) {

    }

    @Override
    public void onLoadNearManAndDynamicTopicFailed() {

    }

    @Override
    public void onLoadQueryIsPerfectFailed() {

    }

    @Override
    public void onLoadQueryIsPerfectSuccess(PerfectBean s) {
    }


}
